package myPackage;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * @author Glenn Mark
 */

class XMLProcessor {
	
	private File xmlPath;
	
	private final DocumentBuilderFactory docBuilder_FACTORY = DocumentBuilderFactory.newInstance();
	private DocumentBuilder docBuilder;
	private Document xml;
	
	private DOMSource XMLinput= null;
	private StreamResult XMLoutput = null;
	
	/** Default Constructor */
	public XMLProcessor(){}
	
	/**
	 * @throws NullPointerException for missing files
	 * */
	public XMLProcessor(File xmlPath) throws NullPointerException {
		this.xmlPath = xmlPath;
	}

	/**
	 * Read existing XML File
	 * */
	public Document XMLReader() throws NullPointerException {
		
		try {
			
			xml = docBuilder_FACTORY.newDocumentBuilder().parse(xmlPath);
			
		} catch (ParserConfigurationException pce) {

			System.err.println("Class  : XMLProcessor");
			System.err.println("Method : readXML");
			System.err.println("try/catch block: ParserConfigurationException ");
			System.err.println("Missing or invalid file!");

		} catch (SAXException e) {

			System.err.println("Class  : XMLProcessor");
			System.err.println("Method : readXML");
			System.err.println("try/catch block: SAXException ");
			System.err.println("Missing or invalid file!");

		} catch (IOException e) {

			System.err.println("Class  : XMLProcessor");
			System.err.println("Method : readXML");
			System.err.println("try/catch block: IOException ");
			System.err.println("Missing or invalid file!");

		}

		return xml;
	}
	
	public Document XMLWriter(File XMLFile) throws NullPointerException {
		
		try {
			
			docBuilder = docBuilder_FACTORY.newDocumentBuilder();
			xml = docBuilder.newDocument();
			
			XMLinput = new DOMSource(xml);
			XMLoutput = new StreamResult(XMLFile);
			getTransformer().transform(XMLinput, XMLoutput);
			
			//ALERTHERE
			System.out.println("File saved!");
			
		} catch (ParserConfigurationException pce) {
			System.err.println("Class  : XMLProcessor");
			System.err.println("Method : readXML");
			System.err.println("try/catch block: ParserConfigurationException ");
		} catch (NullPointerException e) {
			System.err.println("Class  : XMLProcessor");
			System.err.println("Method : readXML");
			System.err.println("try/catch block: NullPointerException ");
		} catch (TransformerException e) {
			System.err.println("Class  : XMLProcessor");
			System.err.println("Method : readXML");
			System.err.println("try/catch block: TransformerException ");
		} 

		return xml;
		
	}
	
	public void XMLEraser(File XMLFile) throws NullPointerException {
		
		try {
			File file = new File(XMLFile.toString());
			
			if (!file.exists()) {
				System.err.println(file + " doesn't exist in the system.");
			}
			
			//ALERTHERE
			if(file.delete()){
				System.out.println(file.getName() + " is deleted!");
			} else {
				System.err.println("Delete operation failed.");
			}
			
		} catch (NullPointerException npe) {
			System.err.println("** Class : XMLProcessor **");
			System.err.println("** Method: XMLEraser **");
			System.err.println("**try/catch : NullPointerException");
			System.err.println("Delete operation failed");
		} 
		
	}
	
	/**
	 * Write to user's data file ($_HOME/PID.xml)
	 * 
	 * <p>Actions accepted : </p>
	 * <table border="1" bordercolor="#003300" width="50%" cellpadding="3" cellspacing="3">
	 * <tr>
	 * 	<th>Action</th>
	 * 	<th>Description</th>
	 * </tr>
	 * 
	 * <tr>
	 * 	<td>newScoreSheet</td>
	 * 	<td>write new user scoresheet</td>
	 * </tr>
	 * 
	 * <tr>
	 * <td>scoreDetail</td>
	 * <td>write user score details</td>
	 * </tr>
	 * 
	 * <tr>
	 * <td>addUser</td>
	 * <td>write (add) new user to users data file</td>
	 * </tr>
	 * 
	 * </table>
	 * 
	 * @param action - Specify the action to be performed
	 * @param integer - the User's PID
	 * 
	 * */
	
	public Transformer getTransformer() throws TransformerConfigurationException {
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = null;
		
		try {
			
			transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
			
		} catch (TransformerConfigurationException e) {
			System.err.println("** Class : XMLProcessor **");
			System.err.println("** Method: getTransformer() **");
			System.err.println("** try/catch : TransformerConfigurationError");
			System.err.println("** unknown error **");
		}
		
		return transformer;
		
	}

	
	// =================== Future Method(s)

}